/*
 * TCPSocketSSLRev.cpp
 *
 *  Created on: 2011-3-5
 *      Author: lionxing
 */

#include "TCPSocketSSLRev.h"
#include <string.h>

CTCPSocketSSLRev::CTCPSocketSSLRev(MObserverRevSSL& aObserver, CSecureSocket* aSocket) :
CActive(EPriorityStandard),	iObserver(aObserver), iSSLRev(aSocket),
		iRecvBuf(0,0,0)// Standard priority
{
	CActiveScheduler::Add(this);				// Add to scheduler
	iBuffPtr = new (ELeave) TUint8[KBuffSizeSOCKETSSL];

}

CTCPSocketSSLRev::~CTCPSocketSSLRev()
{
	Cancel(); 

	delete iBuffPtr;
}

void CTCPSocketSSLRev::Receive()
{
	Cancel();
	// Receive the length of the message
	memset(iBuffPtr,0,KBuffSizeSOCKETSSL);
	iRecvBuf.Set(iBuffPtr, 0, KBuffSizeSOCKETSSL);	
	iSSLRev->RecvOneOrMore(iRecvBuf, iStatus, iLen);
	SetActive();
}

void CTCPSocketSSLRev::DoCancel()
{
	iSSLRev->CancelRecv();
}

void CTCPSocketSSLRev::RunL()
{
	TInt err = iStatus.Int();
	if(err==KErrNone)
		{		

		TInt recvLen = iLen();					
		iRecvBuf.Set(iBuffPtr, recvLen, recvLen);
		iObserver.OnReceiveSSL(iRecvBuf, err);
		}
	else
		{
	this->iObserver.OnReceiveSSL(KNullDesC8(),err);
		}	
}

